<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use alidms\api_demo\SmsDemo;

class Login extends Controller
{
    // 登录页
    public function index()
    {
        $this->view->engine->layout(false);

        return view("/login/login");
    }

    // 用户登录
    public function login(Request $request)
    {
        $data = $request->param();

        $rule = [
            'username|用户名' => 'require|token',
            'password|密码' => 'require'
        ];

        $validate = new \think\Validate($rule);

        if(!$validate->check($data))
        {
            $this->error($validate->getError());
            die;
        }

        $pwd = indexpwd($data['password']);
        // 判断用户是否存在
        $res = \app\index\model\User::where("phone",$data['username'])->find();

        // 连接rediss
        $redis = new \Redis();
        $redis->connect('127.0.0.1','6379');

        $count = $redis->get($data['username']);

        if($count >= 3)
        {
            echo '你的密码已经输错三次';
            die;

        }else{
            if($res['password'] == $pwd && $res['is_check'] == 1 && $res){

                // 使用set类型记录记录每天用户登录次数
                $redis->sAdd(date('m-d').'action_num',$data['username']);

                // 使用双向列表记录每天前十位用户
                $redis_login_list = $redis->lRange("tpshop_first_login",0,$redis->lLen('tpshop_first_login')-1);

                // 判断是否重复登录
                if(!in_array($data['username'],$redis_login_list))
                {
                    $redis->rPush('tpshop_first_login',$data['username']);
                }
                $redis->expire('tpshop_first_login',3600*12);

                // 登陆成功之后存储到session中
                session("index_user",$res->toArray());

                $this->success("登陆成功，正在跳转","index/index/index");

            }else{

                $redis->incr($data['username']);
                $redis->expire($data['username'],8);

                $this->error("登陆失败，账号或者密码错误，请重新登陆","index/login/index");

            }
        }
    }

    // 手机注册页面
    public function register()
    {

        $this->view->engine->layout(false);

        return view("/login/register");
    }

    // 添加页面
    public function insert(Request $request)
    {
        $data = $request->param();

        // 表单验证
        $rule = [
            'phone|手机号' => 'require|token|number|max:11',
            'code|验证码' => 'require|number|max:4',
            'password|密码' => 'require|confirm'
        ];
        $validate = new \think\Validate($rule);

        if(!$validate->check($data)){
            $this->error($validate->getError());
        }

        // 查询表中是否存在当前手机号
        $res_phone = \app\index\model\User::where("phone",$data['phone'])->find();
        if(!empty($res_phone)){

            $this->error("手机号重复",'index/Login/register');
            die;
        }

        // 获取存储在缓存中的验证码
        $code = cache("reg_code");

        // 判断验证码是否相等
        if($code != $data['code']){

            $this->error("验证码错误",'index/Login/register');
            die;
        }

        // 调用成功之后删除缓存中的验证码
        cache("reg_code",null);

        $data['username'] = $data['phone'];
        $data['is_check'] = 1;
        $data['password'] = indexpwd($data['password']);
        $data['last_time_login'] = strtotime(date("Y-m-d h:i:s"));

        \app\index\model\User::create($data,true);

        $this->success("添加成功","index/Login/index");


    }

    // 手机注册验证码
    public function sendsms(Request $request)
    {
        $data = $request->param();

        if(empty($data) && is_int($data['phone'])){

            $res = [
                'code' => 10001,
                'msg' => '格式错误'
            ];

            return json($res);
        }

        // 获取的手机号
        $phone = $data['phone'];

        // 查询表中是否存在当前手机号
        $res_phone = \app\index\model\User::where("phone",$phone)->find();
        if(!empty($res_phone)){

            $res = [
                'code' => 10004,
                'msg' => "手机号重复"
            ];
            return $res;
            die;
        }

        // 随机验证码
        $code = mt_rand(1000,9999);

        $data = SmsDemo::sendSms($phone,$code);

        if($data->Code == "OK"){

            cache("reg_code",$code,120);

            $res = [
                'code' => 10000,
                'msg' => "成功"
            ];
            return $res;
        }else{
            $res = [
                'code' => 10003,
                'msg' => "失败"
            ];
            return $res;
        }

    }

    // 邮箱注册页面
    public function regEmail()
    {
        $this->view->engine->layout(false);

        return view('/login/register-email');
    }

    // 退出
    public function logout(){

        session("index_user",null);

        $this->success("退出成功",'index/index/index');

    }
}
